package fhp.hlhj.giantfold.adapter

import android.content.Context
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.bumptech.glide.Glide
import de.hdodenhof.circleimageview.CircleImageView
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.javaBean.FriendsEFBean
import fhp.hlhj.giantfold.network.Urls
import java.text.DecimalFormat
import java.util.ArrayList

/**
 * Created by Administrator on 2018\2\6 0006.
 */
class FriendsEfAdp(private val context:Context,private val datas:ArrayList<FriendsEFBean.DataBean>):BaseAdapter() {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var converView=convertView
        if (converView==null){
            converView=LayoutInflater.from(context).inflate(R.layout.firendsef_item,null)
        }
        val bean = datas[position]
        val itemName = converView?.findViewOften<TextView>(R.id.itemNick)
        val itemNum = converView?.findViewOften<TextView>(R.id.itemNum)
        val itemIcon = converView?.findViewOften<CircleImageView>(R.id.itemIcon)
        itemName?.text = bean.nick_name
        val format = DecimalFormat("0.00")
        itemNum?.text=format.format(bean.earnings_accumulated.toDouble()).toString()
        Glide.with(context).load(Urls.BaseUrl+"/"+bean.savepath+bean.savename).asBitmap().dontAnimate().placeholder(R.mipmap.placeholdericon).into(itemIcon)

        return converView!!
    }

    override fun getItem(position: Int): Any {
        return datas[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return  datas.size
    }
    fun <T : View> View.findViewOften(viewId: Int): T {
        var viewHolder: SparseArray<View> = tag as? SparseArray<View> ?: SparseArray()
        tag = viewHolder
        var childView: View? = viewHolder.get(viewId)
        if (null == childView) {
            childView = findViewById(viewId)
            viewHolder.put(viewId, childView)
        }
        return childView as T
    }
}